package com.devicemantra.coordinate.interfaces.call;

import java.io.IOException;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.devicemantra.coordinate.interfaces.email.CSendEmail;
import com.devicemantra.coordinate.ds.tests.PMF;
import com.devicemantra.coordinate.util.CLogger;

public class getVoiceMessage extends HttpServlet 
{
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
	throws IOException 
	{
		doGet(req,resp);
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws IOException {
	
		String lMessageId = req.getParameter("id");
		System.out.println("Id we got was " + lMessageId);
		
		boolean lDoesExist = true;
		String lCallMesgId = lMessageId;
		PersistenceManager lPMF = PMF.get().getPersistenceManager();
		CCallMessage lCMessage = null;
		try
		{
			lCMessage = lPMF.getObjectById(CCallMessage.class,lCallMesgId);
		} catch(JDOObjectNotFoundException e)
		{
			lDoesExist = false;
		}
		
		if(false == lDoesExist)
		{
			System.out.println("FATAL ERROR: The message id does not exist :" + lCallMesgId);
			return;
		}
		System.out.println("The message retreived is " + lCMessage.getMessage());
		System.out.println("Serving message to Twilio");
		String lResponseXml = "<?xml version='1.0' encoding='utf-8' ?>"
								+ "<Response>" 
								+ "<Say voice='woman'>" 
								+ lCMessage.getMessage() 
								+ "</Say>"
								+ "</Response>";
		resp.setContentType("text/xml");
		resp.getOutputStream().print(lResponseXml);
		System.out.println("Response : " + lResponseXml);
		
		
	}
}
